#include <iostream>


void func1() { std::cout << "f1\n"; }
void func2() { std::cout << "f2\n"; }


struct Person {
    int age;
    char name[20];
};



int a = 10;
int b = 20;
/**
 * 内存4区
 */
void Test01(){
    //全局变量在数据区
    std::cout << "=========全局区==============" << std::endl;
    std::cout << "全局变量a的地址是:" << &a << std::endl;
    std::cout << "全局变量b的地址是:" << &b << std::endl;


    //局部变量在栈区
    int c = 30;
    int d = 40;
    std::cout << "=========栈区==============" << std::endl;
    std::cout << "局部变量c的地址是:" << &c << std::endl;
    std::cout << "局部变量d的地址是:" << &d << std::endl;
   

    int* arr = new int[5]; //动态分配内存，数组在堆区
    std::cout << "=========堆区==============" << std::endl;
    std::cout << "动态分配的数组的地址是:" << arr << std::endl;

    Person* person = new Person; //动态分配内存，结构体在堆区
    person->age = 25;
    person->name[0] = 'J';
    person->name[1] = 'K';
    person->name[2] = '\0'; //字符串结束符
    std::cout << "动态分配的结构体的地址是:" << person << std::endl;

    //释放动态分配的内存
    delete[] arr; //释放动态分配的数组
    delete person; //释放动态分配的结构体


    //代码区
    std::cout << "=========代码区==============" << std::endl;
    std::cout << "函数func1的地址是:" << (void*)func1 << std::endl;
    std::cout << "函数func2的地址是:" << (void*)func2 << std::endl;
}

//通过指针修改堆区的数据
void Test02(){
    Person* person = new Person; //动态分配内存，结构体在堆区
    person->age = 25;
    person->name[0] = 'J';
    person->name[1] = 'K';
    person->name[2] = '\0'; //字符串结束符

    std::cout << "修改前: " << person->name << ", 年龄: " << person->age << std::endl;

    //通过指针修改堆区的数据
    person->age = 30;
    person->name[0] = 'L';

    std::cout << "修改后: " << person->name << ", 年龄: " << person->age << std::endl;

    delete person; //释放动态分配的内存
}

//数组分配在堆上
void Test03(){
    int* arr = new int[5];
    for (int i = 0; i < 5; ++i) {
        arr[i] = i; // 初始化数组
    }

    for (int i = 0; i < 5; ++i) {
        std::cout << arr[i] << " "; // 输出数组元素
    }
    std::cout << std::endl;
    delete[] arr;
}




int main(){
    // Test01();

    // Test02();

    return 0;
}